/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * PTDRecorderThread.java
 *
 * Created on April 6, 2007, 12:49 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package ptd;

import ssi_colorizer.*;
import java.io.*;
import ssi_utilities.ssi_executor;

/**
 *
 * @author John
 */
public class PTDRecorderThread extends java.lang.Thread {
    
    private PTDScriptPane myPane;
    
    /** Creates a new instance of PTDRecorderThread */
    public PTDRecorderThread(PTDScriptPane inPane) {
        myPane = inPane;
    }
    
    public void run(){
        Runtime rt = Runtime.getRuntime();
        try{
            Process proc = ssi_executor.Execute(Main.getAppDir() + "/recorder.exe");
            if(proc == null){
                PTDMessageBox aMsg = new PTDMessageBox("Cannot find recorder.exe.  Reinstall PTD.");
                aMsg.setVisible(true);
            }
            //String RecProc = Main.getAppDir() + "/recorder.exe";
            //Process proc = rt.exec(RecProc);
            
            // This seems to be the only one that will record real time...
            InputStream is = proc.getInputStream();
            int len;
            byte buf[] = new byte[1000];
            try {
                while( (len = is.read(buf)) != -1 ) {
                    String str = new String(buf,0,0,len);
                    //Main.debug_out( "Process out: " + str );
                    myPane.addText(str);
                }
            }  catch (IOException e) {
                Main.debug_out(e.getMessage());
                PTDMessageBox aMsg = new PTDMessageBox("Could not run recorder.  Reinstall PTD.");
                aMsg.setVisible(true);
            }
            
            
        }catch(Exception IOEx){
            Main.debug_out(IOEx.getMessage());
            PTDMessageBox aMsg = new PTDMessageBox("Could not run recorder.  Reinstall PTD.");
            aMsg.setVisible(true);
        }

    }

}
